var xData = ["2015年", "2016年", "2017年", "2018年", "2019年"]
var orgs = ['北京大学','同济大学','北京大学','北京大学']
var len = orgs.length
var data = [
    {
        year: xData,
        data: {
            lobal_introduction:  [122,232,12,32,12],
            domestic_introduction: [12,23,21,22,11],
            abroad_introduction: [12,23,21,22,11],
            org_name: '清华大学'
        }
    },
    {
        year: xData,
        data: {
            lobal_introduction:  [122,232,12,32,12],
            domestic_introduction: [12,23,21,22,11],
            abroad_introduction: [12,23,21,22,11],
            org_name: '清华大学1'
        }
    },
    ]
if(len>4){
    orgs = orgs.slice(0,4)
}
// console.log(orgs)

function titleData(orgs){
    var titleList = [
        {
            text: '标题',
            left: 'center',
            textStyle: {
                color: '#000',
                fontSize: 26
            }
        }]
    let len = orgs.length
        if (len == 1){
            let org = orgs[0]
            let l = org.length
            let x = (50-l*0.75) + '%'
            console.log(x)
        } else if(len == 2){
            let len1 = orgs[0].length
            let len2 = orgs[1].length
            let x1 = (26-len1/2) + '%'
            let x2 = (26-len2/2) + '%'
            console.log(x)
            titleList.push({
                left: x1,
                text: orgs[0],
                bottom: 0
            })
            titleList.push({
                right: x2,
                text: orgs[1],
                bottom: 0
            })
            console.log(titleList)
        } else if (len==3) {
            let len1 = orgs[0].length
            let len2 = orgs[1].length
            let len3 = orgs[2].length
            let x1 = (19-len1*0.75) + '%'
            let x2 = (49-len2*0.75) + '%'
            let x3 = (79-len3*0.75) + '%'
            console.log(x1)
            console.log(x2)
            console.log(x3)
            titleList.push({
                left: x1,
                text: orgs[0],
                bottom: 0
            })
            titleList.push({
                left: x2,
                text: orgs[1],
                bottom: 0
            })
            titleList.push({
                left: x3,
                text: orgs[2],
                bottom: 0
            })
        }else if(len==4){
            let len1 = orgs[0].length
            let len2 = orgs[1].length
            let len3 = orgs[2].length
            let len4 = orgs[3].length
            let x1 = (12-len1*0.75) + '%'
            let x2 = (36-len2*0.75) + '%'
            let x3 = (60-len3*0.75) + '%'
            let x4 = (84-len4*0.75) + '%'
            console.log(x1)
            console.log(x2)
            console.log(x3)
            console.log(x4)
            titleList.push({
                left: x1,
                text: orgs[0],
                bottom: 0
            })
            titleList.push({
                left: x2,
                text: orgs[1],
                bottom: 0
            })
            titleList.push({
                left: x3,
                text: orgs[2],
                bottom: 0
            })
            titleList.push({
                left: x4,
                text: orgs[3],
                bottom: 0
            })
        }
    return titleList
}
console.log(titleData(orgs))

// 已完成
function xAxisData(orgs) {
    var xData = []
    let xtype = {
        type:"category",
        axisLabel: { rotate: "90" },
        splitLine: {
          show: true,
          lineStyle: {
            type: "dashed",
          },
        },
    }
    let len = orgs.length
    for (let i=0; i<len;i++){
        xtype.data = xData
        xtype.gridIndex = i
        xData.push(xtype)
    }
    return xData
}

// 已完成
function yAxisData(orgs) {
    var yData = []
    var yAxis = {
      type: "value",
      axisLine: {
        show: false,
      },
      axisTick: {
        show: false,
      },
      axisLabel: {
        show: false,
      },
      splitLine: { show: false }
    }
    for (let i=0;i<orgs.length;i++){
        yAxis.gridIndex = i
        yData.push(yAxis)
    }
    return yData
}

// 已完成
function gridData(len) {
    var grid = []
    if (len==1){
        grid = [{
          top: "15%",
          bottom: "15%",
          left: '10%',
          right: "10%",
        }]
    } else if (len==2){
        grid =  [
        {
          top: "15%",
          bottom: "15%",
          left: '10%',
          right: "55%",
        },
        {
          top: "15%",
          bottom: "15%",
          left: "55%",
          right: '10%',
        }
      ]
    } else if(len==3) {
        grid =  [
    {
        top: "15%",
        bottom: "15%",
        x: '7.5%',
        width: '25%',
    },
    {
        top: "15%",
        bottom: "15%",
        x: '37.5%',
        width: '25%',
    },{
        top: "15%",
        bottom: "15%",
        x: '67.5%',
        width: '25%',
    },
    ]
    } else if (len==4) {
        grid = [
            {
                //   left: '10%',
                //   right: "10%",
                // y: '2%',
                top: "15%",
                bottom: "15%",
                x: '1%',
                width: '23%',
            },
            {
                //   left: "55%",
                //   right: '10%',
                // y: '2%',
                top: "15%",
                bottom: "15%",
                x: '25%',
                width: '23%',
            },{
                top: "15%",
                bottom: "15%",
                x: '49%',
                width: '23%',
                //   left: "55%",
                //   right: '10%',
                // y: '2%',
            },{
                top: "15%",
                bottom: "15%",
                x: '73%',
                width: '23%',
                //   left: "55%",
                //   right: '10%',
                // y: '2%',
            },
        ]
        // let g = {top: "15%",bottom: "15%"}
        // let x = '1%'
        // let width = '23%'
    }
    return grid
}

// 完成
function seriesData(data){
    var series = []
    console.log(data)
    for (let i=0;i<data.length;i++) {
        let stype = {
            type: 'bar',
            showSymbol: false,
            barWidth: '40%',
            label: {
                show: true,
                position: 'inside'
            },
            stack: data[i].data.org_name,
            yAxisIndex:i,
            xAxisIndex:i,
        }
        stype.data = data[i].data.lobal_introduction
        series.push(stype)
        stype.data = data[i].data.domestic_introduction
        series.push(stype)
        stype.data = data[i].data.abroad_introduction
        series.push(stype)
    }
    return series
}

// console.log(seriesData(data))

option = {
    title:  [
        {
            text: '标题',
            left: 'center',
            textStyle: {
                color: '#000',
                fontSize: 32,
                fontWeight:800
            }
        },
      {
        left: "9%", // 12
        text: "北京大学",
        bottom: 0,
      },
      {
        left: "33%", //36
        text: "北京大学",
        bottom: 0,
      }, {
        left: "57%", // 60
        text: "北京大学",
        bottom: 0,
      },{
        left: "81%", //84
        text: "北京大学",
        bottom: 0,
      },
    ],
    tooltip: {
        trigger: "axis",
    },
    toolbox: {
        show: true,
        orient: 'vertical',
        y: 'center',
        right: '10px',
        feature: {
          make: {show: true},
          dataView: {show:true,readOnly: false},
          magicType: {show:true,type: ['line','bar',]},
          restore: {show:true},
          saveAsImage: {show: true}
        }
      },
    xAxis: [
        {
            type:"category",
            axisLabel: { rotate: "90" },
            splitLine: {
              show: true,
              lineStyle: {
                type: "dashed",
              },
            },
            data: xData,
            gridIndex:0
        },
        {
            type:"category",
            axisLabel: { rotate: "90" },
            splitLine: {
              show: true,
              lineStyle: {
                type: "dashed",
              },
            },
            data: xData,
            gridIndex:1
        },  {
            type:"category",
            axisLabel: { rotate: "90" },
            splitLine: {
              show: true,
              lineStyle: {
                type: "dashed",
              },
            },
            data: xData,
            gridIndex:2
        }, {
            type:"category",
            axisLabel: { rotate: "90" },
            splitLine: {
              show: true,
              lineStyle: {
                type: "dashed",
              },
            },
            data: xData,
            gridIndex:3
        },
    ],
    yAxis: [
    {
      type: "value",
      axisLine: {
        show: false,
      },
      axisTick: {
        show: false,
      },
      axisLabel: {
        show: false,
      },
      splitLine: { show: false },
    },
    {
      type: "value",
      axisLine: {
        show: false,
      },
      axisTick: {
        show: false,
      },
      axisLabel: {
        show: false,
      },
      splitLine: { show: false },
      gridIndex: 1,
    },{
      type: "value",
      axisLine: {
        show: false,
      },
      axisTick: {
        show: false,
      },
      axisLabel: {
        show: false,
      },
      splitLine: { show: false },
      gridIndex: 2,
    },{
      type: "value",
      axisLine: {
        show: false,
      },
      axisTick: {
        show: false,
      },
      axisLabel: {
        show: false,
      },
      splitLine: { show: false },
      gridIndex: 3,
    },
    ],
    grid: [
    {
        //   left: '10%',
        //   right: "10%",
        // y: '2%',
        top: "15%",
        bottom: "15%",
        x: '1%',
        width: '23%',
    },
    {
        //   left: "55%",
        //   right: '10%',
        // y: '2%',
        top: "15%",
        bottom: "15%",
        x: '25%',
        width: '23%',
    },{
        top: "15%",
        bottom: "15%",
        x: '49%',
        width: '23%',
        //   left: "55%",
        //   right: '10%',
        // y: '2%',
    },{
        top: "15%",
        bottom: "15%",
        x: '73%',
        width: '23%',
        //   left: "55%",
        //   right: '10%',
        // y: '2%',
    },
    ],
    series: [{
        type: 'bar',
        showSymbol: false,
        barWidth: '50%',
        label: {
            show: true,
            position: 'inside'
        },
        stack:'清华大学',
        data: [122,232,12,32,12]
      },
      {
          type: 'bar',
          showSymbol: false,
          barWidth: '50%',
          stack:'清华大学',
          label: {
              show: true,
              position: 'inside'
          },
          data: [12,23,21,22,11]
      },
      {
          type: 'bar',
          showSymbol: false,
          stack:'北京大学',
          barWidth: '50%',
          label: {
              show: true,
              position: 'inside'
          },
          data: [111,222,33,411,22],
          xAxisIndex: 1,
          yAxisIndex: 1
      },
      {
          type: 'bar',
          showSymbol: false,
          barWidth: '50%',
          label: {
              show: true,
              position: 'inside'
          },
          stack:'北京大学',
          data: [12,22,32,32,11],
          xAxisIndex: 1,
          yAxisIndex: 1
      }, {
          type: 'bar',
          showSymbol: false,
          stack:'北京大学1',
          barWidth: '50%',
          label: {
              show: true,
              position: 'inside'
          },
          data: [111,222,33,411,22],
          xAxisIndex: 2,
          yAxisIndex: 2
      },
      {
          type: 'bar',
          showSymbol: false,
          barWidth: '50%',
          label: {
              show: true,
              position: 'inside'
          },
          stack:'北京大学1',
          data: [12,22,32,32,11],
          xAxisIndex: 2,
          yAxisIndex: 2
      },{
          type: 'bar',
          showSymbol: false,
          stack:'北京大学14',
          barWidth: '50%',
          label: {
              show: true,
              position: 'inside'
          },
          data: [111,222,33,411,22],
          xAxisIndex: 3,
          yAxisIndex: 3
      },
      {
          type: 'bar',
          showSymbol: false,
          barWidth: '50%',
          label: {
              show: true,
              position: 'inside'
          },
          stack:'北京大学14',
          data: [12,22,32,32,11],
          xAxisIndex: 3,
          yAxisIndex: 3
      },
    ]
    };
